<!--
Copyright Louis Dionne 2013-2016
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
-->
<!-- boost-no-inspect -->
<!-- HTML header for doxygen 1.8.9.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Boost.Hana: Monoid</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
MathJax.Hub.Config({
    "HTML-CSS": {
        linebreaks: {
            automatic: true,
            width: "75% container"
        }
    }
});
</script><script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<!-- Additional javascript for drawing charts. -->
<script type="text/javascript" src="highcharts.js"></script>
<script type="text/javascript" src="highcharts-data.js"></script>
<script type="text/javascript" src="highcharts-exporting.js"></script>
<script type="text/javascript" src="chart.js"></script>
<script type="text/javascript" src="hana.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Boost.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">Boost.Hana
   &#160;<span id="projectnumber">1.0.2</span>
   </div>
   <div id="projectbrief">Your standard library for metaprogramming</div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)"
               onblur="searchBox.OnSearchFieldFocus(false)"
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;"
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('group__group-Monoid.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="summary">
<a href="#var-members">Variables</a>  </div>
  <div class="headertitle">
<div class="title">Monoid<div class="ingroups"><a class="el" href="group__group-concepts.html">Concepts</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<a name="details" id="details"></a><h2 class="groupheader">Description</h2>
<p>The <code>Monoid</code> concept represents data types with an associative binary operation that has an identity. </p>
<p>Specifically, a <a href="http://en.wikipedia.org/wiki/Monoid">Monoid</a> is a basic algebraic structure typically used in mathematics to construct more complex algebraic structures like <code>Group</code>s, <code>Ring</code>s and so on. They are useful in several contexts, notably to define the properties of numbers in a granular way. At its core, a <code>Monoid</code> is a set <code>S</code> of objects along with a binary operation (let's say <code>+</code>) that is associative and that has an identity in <code>S</code>. There are many examples of <code>Monoid</code>s:</p><ul>
<li>strings with concatenation and the empty string as the identity</li>
<li>integers with addition and <code>0</code> as the identity</li>
<li>integers with multiplication and <code>1</code> as the identity</li>
<li>many others...</li>
</ul>
<p>As you can see with the integers, there are some sets that can be viewed as a monoid in more than one way, depending on the choice of the binary operation and identity. The method names used here refer to the monoid of integers under addition; <code>plus</code> is the binary operation and <code>zero</code> is the identity element of that operation.</p>
<h2>Minimal complete definition </h2>
<p><code>plus</code> and <code>zero</code> satisfying the laws</p>
<h2>Laws </h2>
<p>For all objects <code>x</code>, <code>y</code> and <code>z</code> of a <code>Monoid</code> <code>M</code>, the following laws must be satisfied: </p><div class="fragment"><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(zero&lt;M&gt;(), x) == x                    <span class="comment">// left zero</span></div><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, zero&lt;M&gt;()) == x                    <span class="comment">// right zero</span></div><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, <a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(y, z)) == <a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, y), z) <span class="comment">// associativity</span></div></div><!-- fragment --><h2>Concrete models </h2>
<p><code><a class="el" href="structboost_1_1hana_1_1integral__constant.html" title="Compile-time value of an integral type. ">hana::integral_constant</a></code></p>
<h2>Free model for non-boolean arithmetic data types </h2>
<p>A data type <code>T</code> is arithmetic if <code>std::is_arithmetic&lt;T&gt;::value</code> is true. For a non-boolean arithmetic data type <code>T</code>, a model of <code>Monoid</code> is automatically defined by setting </p><div class="fragment"><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, y) = (x + y)</div><div class="line">zero&lt;T&gt;() = <span class="keyword">static_cast&lt;</span>T<span class="keyword">&gt;</span>(0)</div></div><!-- fragment --><blockquote class="doxtable">
<h4>Rationale for not making <code>bool</code> a <code>Monoid</code> by default</h4>
<p>First, it makes no sense whatsoever to define an additive <code>Monoid</code> over the <code>bool</code> type. Also, it could make sense to define a <code>Monoid</code> with logical conjunction or disjunction. However, C++ allows <code>bool</code>s to be added, and the method names of this concept really suggest addition. In line with the principle of least surprise, no model is provided by default. </p>
</blockquote>
<h2>Structure-preserving functions </h2>
<p>Let <code>A</code> and <code>B</code> be two <code>Monoid</code>s. A function <code>f : A -&gt; B</code> is said to be a <a href="http://en.wikipedia.org/wiki/Monoid#Monoid_homomorphisms">Monoid morphism</a> if it preserves the monoidal structure between <code>A</code> and <code>B</code>. Rigorously, for all objects <code>x, y</code> of data type <code>A</code>, </p><div class="fragment"><div class="line">f(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, y)) == <a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(f(x), f(y))</div><div class="line">f(<a class="code" href="group__group-Monoid.html#gad459ac17b6bab8ead1cae7de0032f3c6">zero</a>&lt;A&gt;()) == <a class="code" href="group__group-Monoid.html#gad459ac17b6bab8ead1cae7de0032f3c6">zero</a>&lt;B&gt;()</div></div><!-- fragment --><p> Functions with these properties interact nicely with <code>Monoid</code>s, which is why they are given such a special treatment.</p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem:gaeb5d4a1e967e319712f9e4791948896c"><td class="memItemLeft" align="right" valign="top">constexpr auto&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">boost::hana::plus</a></td></tr>
<tr class="memdesc:gaeb5d4a1e967e319712f9e4791948896c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Associative binary operation on a <code>Monoid</code>.  <a href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">More...</a><br /></td></tr>
<tr class="separator:gaeb5d4a1e967e319712f9e4791948896c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gad459ac17b6bab8ead1cae7de0032f3c6"><td class="memTemplParams" colspan="2">template&lt;typename M &gt; </td></tr>
<tr class="memitem:gad459ac17b6bab8ead1cae7de0032f3c6"><td class="memTemplItemLeft" align="right" valign="top">constexpr auto&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="group__group-Monoid.html#gad459ac17b6bab8ead1cae7de0032f3c6">boost::hana::zero</a></td></tr>
<tr class="memdesc:gad459ac17b6bab8ead1cae7de0032f3c6"><td class="mdescLeft">&#160;</td><td class="mdescRight">Identity of <code>plus</code>.  <a href="group__group-Monoid.html#gad459ac17b6bab8ead1cae7de0032f3c6">More...</a><br /></td></tr>
<tr class="separator:gad459ac17b6bab8ead1cae7de0032f3c6"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<h2 class="groupheader">Variable Documentation</h2>
<a class="anchor" id="gaeb5d4a1e967e319712f9e4791948896c"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">constexpr auto boost::hana::plus</td>
        </tr>
      </table>
</div><div class="memdoc">

<p><code>#include &lt;<a class="el" href="fwd_2plus_8hpp.html">boost/hana/fwd/plus.hpp</a>&gt;</code></p>
<b>Initial value:</b><div class="fragment"><div class="line">= [](<span class="keyword">auto</span>&amp;&amp; x, <span class="keyword">auto</span>&amp;&amp; y) -&gt; decltype(<span class="keyword">auto</span>) {</div><div class="line">        <span class="keywordflow">return</span> tag-dispatched;</div><div class="line">    }</div></div><!-- fragment -->
<p>Associative binary operation on a <code>Monoid</code>. </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">x,y</td><td>Two objects to combine with the <code>Monoid</code>'s binary operation.</td></tr>
  </table>
  </dd>
</dl>
<h2>Cross-type version of the method </h2>
<p>The <code>plus</code> method is "overloaded" to handle distinct data types with certain properties. Specifically, <code>plus</code> is defined for <em>distinct</em> data types <code>A</code> and <code>B</code> such that</p><ol type="1">
<li><code>A</code> and <code>B</code> share a common data type <code>C</code>, as determined by the <code>common</code> metafunction</li>
<li><code>A</code>, <code>B</code> and <code>C</code> are all <code>Monoid</code>s when taken individually</li>
<li><code>to&lt;C&gt; : A -&gt; B</code> and <code>to&lt;C&gt; : B -&gt; C</code> are <code>Monoid</code>-embeddings, as determined by the <code>is_embedding</code> metafunction.</li>
</ol>
<p>The definition of <code>plus</code> for data types satisfying the above properties is obtained by setting </p><div class="fragment"><div class="line"><a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(x, y) = <a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">plus</a>(to&lt;C&gt;(x), to&lt;C&gt;(y))</div></div><!-- fragment --><h2>Example </h2>
<div class="fragment"><div class="line"><span class="comment">// Copyright Louis Dionne 2013-2016</span></div><div class="line"><span class="comment">// Distributed under the Boost Software License, Version 1.0.</span></div><div class="line"><span class="comment">// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="assert_8hpp.html">boost/hana/assert.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="equal_8hpp.html">boost/hana/equal.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="integral__constant_8hpp.html">boost/hana/integral_constant.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="plus_8hpp.html">boost/hana/plus.hpp</a>&gt;</span></div><div class="line"><span class="keyword">namespace </span>hana = <a class="code" href="namespaceboost_1_1hana.html">boost::hana</a>;</div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="group__group-assertions.html#ga4bf9e0c46c44e21fbe5c5fbb3ace8356">BOOST_HANA_CONSTANT_CHECK</a>(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">hana::plus</a>(hana::int_c&lt;3&gt;, hana::int_c&lt;5&gt;) == hana::int_c&lt;8&gt;);</div><div class="line">static_assert(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">hana::plus</a>(1, 2) == 3, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line">static_assert(<a class="code" href="group__group-Monoid.html#gaeb5d4a1e967e319712f9e4791948896c">hana::plus</a>(1.5f, 2.4) == 3.9, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main() { }</div></div><!-- fragment -->
</div>
</div>
<a class="anchor" id="gad459ac17b6bab8ead1cae7de0032f3c6"></a>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename M &gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">constexpr auto boost::hana::zero</td>
        </tr>
      </table>
</div><div class="memdoc">

<p><code>#include &lt;<a class="el" href="fwd_2zero_8hpp.html">boost/hana/fwd/zero.hpp</a>&gt;</code></p>
<b>Initial value:</b><div class="fragment"><div class="line">= []() -&gt; decltype(<span class="keyword">auto</span>) {</div><div class="line">        <span class="keywordflow">return</span> tag-dispatched;</div><div class="line">    }</div></div><!-- fragment -->
<p>Identity of <code>plus</code>. </p>
<dl class="tparams"><dt>Template Parameters</dt><dd>
  <table class="tparams">
    <tr><td class="paramname">M</td><td>The tag (a <code>Monoid</code>) of the returned identity.</td></tr>
  </table>
  </dd>
</dl>
<h2>Example </h2>
<div class="fragment"><div class="line"><span class="comment">// Copyright Louis Dionne 2013-2016</span></div><div class="line"><span class="comment">// Distributed under the Boost Software License, Version 1.0.</span></div><div class="line"><span class="comment">// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="assert_8hpp.html">boost/hana/assert.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="equal_8hpp.html">boost/hana/equal.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="integral__constant_8hpp.html">boost/hana/integral_constant.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="zero_8hpp.html">boost/hana/zero.hpp</a>&gt;</span></div><div class="line"><span class="keyword">namespace </span>hana = <a class="code" href="namespaceboost_1_1hana.html">boost::hana</a>;</div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="group__group-assertions.html#ga4bf9e0c46c44e21fbe5c5fbb3ace8356">BOOST_HANA_CONSTANT_CHECK</a>(<a class="code" href="group__group-Monoid.html#gad459ac17b6bab8ead1cae7de0032f3c6">hana::zero</a>&lt;hana::integral_constant_tag&lt;int&gt;&gt;() == hana::int_c&lt;0&gt;);</div><div class="line">static_assert(hana::zero&lt;long&gt;() == 0l, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main() { }</div></div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!--
Copyright Louis Dionne 2013-2016
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
-->
<!-- boost-no-inspect -->
<!-- HTML footer for doxygen 1.8.9.1-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
  </ul>
</div>
</body>
</html>
